package cn.wolfcode.controller;

import cn.wolfcode.domain.CustomerTransfer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.KeywordQueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.ICustomerTransferService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 客户历史之移交历史
 */
@Api(value = "客户移交历史",description = "客户移交历史资源控制器")
@Controller
@RequestMapping("/customerTransfer")
public class CustomerTransferController {

    @Autowired
    private ICustomerTransferService customerTransferService;

    @Autowired
    private ICustomerService customerService;

    // 处理查询所有部门请求
    @RequestMapping("/list")
    @RequiresPermissions(value = {"customerTransfer:list","客户移交列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") KeywordQueryObject qo){
        PageInfo<CustomerTransfer> pageInfo = customerTransferService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customerTransfer/list";
    }
    // 处理删除请求
    @GetMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            customerTransferService.delete(id);
        }
        return "redirect:/customerTransfer/list.do"; // 让浏览器重新发一次请求
    }
    // 去新增或去修改页面
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id != null) { // 表示去修改
            CustomerTransfer customerTransfer = customerTransferService.get(id);
            model.addAttribute("customerTransfer", customerTransfer);
        }
        return "customerTransfer/input"; // 让浏览器重新发一次请求
    }
    // 新增或修改
    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customerTransfer:saveOrUpdate","客户移交添加/更新"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(CustomerTransfer customerTransfer){
        JsonResult jr = new JsonResult();
        try {
            if(customerTransfer.getId() == null){ // 新增
                //获取用户主体
                Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
                //获取客户的Id
                Long customerId = customerTransfer.getCustomer().getId();
                //获取新销售员的Id
                Long newId = customerTransfer.getNewSeller().getId();
                //进行修改Id
                customerService.updateOldAndNewId(customerId,newId);
                //设置操作人员
                customerTransfer.setOperator(employee);
                //设置操作时间
                customerTransfer.setOperateTime(new Date());
                customerTransferService.save(customerTransfer);
            }else {
                customerTransferService.update(customerTransfer); // 修改
            }
            jr.setFlag(true);
            jr.setMsg("保存成功");
        } catch (Exception e) {
            jr.setFlag(false);
            jr.setMsg("保存失败");
        }
        return jr;
    }
}

